/*
 * Copyright (c) 2022 上海市政工程设计研究总院(集团)有限公司 All Rights Reserved.
 */
package smedi.ismedi.web.bff.convert;


import com.smedi.ismedi.authority.starter.util.VerifyHelper;
import com.smedi.ismedi.constructionproject.api.dto.request.ProjectAssignmentEditRequest;
import com.smedi.ismedi.instrument.api.dto.request.hiddenDanger.QueryQualityHiddenDangerPageRequest;
import com.smedi.ismedi.instrument.api.dto.response.FileResponse;
import com.smedi.ismedi.instrument.api.dto.response.hiddenDanger.QualityHiddenDangerResponse;
import com.smedi.ismedi.personnel.api.dto.request.PersonEditRequest;
import com.smedi.ismedi.personnel.api.dto.response.resp.PersonDetailResp;
import com.smedi.ismedi.safetycheck.dto.request.danger.QueryHiddenDangerByCheckRequest;
import com.smedi.ismedi.safetycheck.dto.response.danger.HiddenDangerResponse;
import com.smedi.ismedi.web.starter.common.PaginationData;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.ReportingPolicy;
import smedi.ismedi.web.bff.dto.AvailableHiddenDangerBySafetyRequest;
import smedi.ismedi.web.bff.dto.ExternalPersonEditRequest;
import smedi.ismedi.web.bff.dto.HiddenDangerOperateDetailResponse;
import smedi.ismedi.web.bff.dto.PersonWithCurrentProjectJobResponse;
import smedi.ismedi.web.bff.dto.quality.AvailableHiddenDangerRequest;
import smedi.ismedi.web.bff.dto.quality.SfcOrQltHiddenDangerResponse;

import java.util.List;

/**
 * AdapterConverter
 *
 * @author 15428
 * &#064;date  2022-06-07 15:22:03
 * @since 0.0.1
 */
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface Converter {
    @Mapping(source = "personId",target = "id")
    PersonEditRequest externalPersonEditRequestToPersonEditRequest(ExternalPersonEditRequest externalPersonEditRequest);

    @Mapping(source = "projectAssignmentId",target = "id")
    ProjectAssignmentEditRequest externalPersonEditRequestToProjectAssignmentEditRequest(ExternalPersonEditRequest externalPersonEditRequest);

    @Mapping(source = "id",target = "personId")
    PersonWithCurrentProjectJobResponse personDetailRespToPersonWithCurrentProjectJobResponse(PersonDetailResp personDetailResp);

    List<PersonWithCurrentProjectJobResponse> personDetailRespToPersonWithCurrentProjectJobResponse(List<PersonDetailResp> personDetailResps);


    PaginationData<SfcOrQltHiddenDangerResponse> hiddenDangerResponseToSfcOrQltHiddenDangerResponse(PaginationData<HiddenDangerResponse> hiddenDangerResponse);

    @Mapping(source = "hiddenDangerStatus",target = "taskStatusName")
    @Mapping(source = "hiddenDangerStatus",target = "taskStatus")
    @Mapping(source = "checkPerson.post",target = "checkPerson.job")
    @Mapping(source = "checkPerson.personId",target = "checkPerson.id")
    @Mapping(source = "position.projectName",target = "project.name")
    @Mapping(source = "position.projectId",target = "project.id")
    @Mapping(source = "position.area",target = "area")
    @Mapping(source = "hiddenDangerCategory",target = "hiddenDangerContentTypeName")
    @Mapping(source = "explainInfo",target = "hiddenDangerExplain")
    @Mapping(source = "hiddenDangerCheckType",target = "checkTypeName")
    @Mapping(source = "hiddenDangerCheckType",target = "checkType")
    @Mapping(source = "rectificationPerson.post",target = "rectificationPerson.job")
    @Mapping(source = "rectificationPerson.personId",target = "rectificationPerson.id")
    @Mapping(source = "organization",target = "organization.name")
    @Mapping(source = "organizationId",target = "organization.id")
    @Mapping(source = "checkTime",target = "checkDate",dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(source = "reviewPerson.post",target = "reviewPerson.job")
    @Mapping(source = "reviewPerson.personId",target = "reviewPerson.id")
    SfcOrQltHiddenDangerResponse hiddenDangerResponseToSfcOrQltHiddenDangerResponse(HiddenDangerResponse hiddenDangerResponse);

    PaginationData<SfcOrQltHiddenDangerResponse> qualityHiddenDangerResponseToSfcOrQltHiddenDangerResponse(PaginationData<QualityHiddenDangerResponse> qualityHiddenDangerResponse);

    SfcOrQltHiddenDangerResponse qualityHiddenDangerResponseToSfcOrQltHiddenDangerResponse(QualityHiddenDangerResponse qualityHiddenDangerResponse);
    @Mapping(source = "hiddenDangerStatus",target = "taskStatusName")
    @Mapping(source = "hiddenDangerStatus",target = "taskStatus")
    @Mapping(source = "checkPerson.post",target = "checkPerson.job")
    @Mapping(source = "checkPerson.personId",target = "checkPerson.id")
    @Mapping(source = "position.projectName",target = "project.name")
    @Mapping(source = "position.projectId",target = "project.id")
    @Mapping(source = "position.area",target = "area")
    @Mapping(source = "hiddenDangerCategory",target = "hiddenDangerContentTypeName")
    @Mapping(source = "explainInfo",target = "hiddenDangerExplain")
    @Mapping(source = "hiddenDangerCheckType",target = "checkTypeName")
    @Mapping(source = "hiddenDangerCheckType",target = "checkType")
    @Mapping(source = "rectificationPerson.post",target = "rectificationPerson.job")
    @Mapping(source = "rectificationPerson.personId",target = "rectificationPerson.id")
    @Mapping(source = "organization",target = "organization.name")
    @Mapping(source = "organizationId",target = "organization.id")
    @Mapping(source = "checkTime",target = "checkDate",dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(source = "reviewPerson.post",target = "reviewPerson.job")
    @Mapping(source = "reviewPerson.personId",target = "reviewPerson.id")
    SfcOrQltHiddenDangerResponse hiddenDangerOperateDetailResponseToSfcOrQltHiddenDangerResponse(HiddenDangerOperateDetailResponse hiddenDangerOperateDetailResponse);

    @Mapping(source = "hiddenDangerStatus",target = "taskStatus")
    QueryQualityHiddenDangerPageRequest availableHiddenDangerRequestToQueryQualityHiddenDangerPageRequest(AvailableHiddenDangerRequest availableHiddenDangerRequest);

    QueryHiddenDangerByCheckRequest availableHiddenDangerRequestToQueryHiddenDangerByCheckRequest(AvailableHiddenDangerRequest availableHiddenDangerRequest);

    @Mapping(source = "checkId",target = "safetyCheckId")
    @Mapping(source = "checkDate",target = "checkTime")
    @Mapping(source = "project.id",target = "position.projectId")
    @Mapping(source = "project.name",target = "position.projectName")
    @Mapping(source = "hiddenDangerTypeName",target = "hiddenDangerType")
    @Mapping(source = "checkTypeName",target = "hiddenDangerCheckType")
    @Mapping(source = "organization.name",target = "organization")
    @Mapping(source = "organization.id",target = "organizationId")
    @Mapping(source = "taskStatusName",target = "hiddenDangerStatus")
    @Mapping(source = "lastOperaPerson.id",target = "lastOperatePerson.personId")
    @Mapping(source = "lastOperaPerson.name",target = "lastOperatePerson.name")
    @Mapping(source = "lastOperaTime",target = "lastOperatePerson.latestUpdatedDate")
    @Mapping(source = "lastOperaTime",target = "latestUpdatedDate")
    @Mapping(target = "lastOperatePerson.role",constant = "最后操作人")
    @Mapping(source = "taskFinishTime",target = "finishDate")
    @Mapping(source = "checkPerson.id",target = "checkPerson.personId")
    @Mapping(source = "checkPerson.job",target = "checkPerson.post")
    @Mapping(source = "rectificationPerson.id",target = "rectificationPerson.personId")
    @Mapping(source = "rectificationPerson.job",target = "rectificationPerson.post")
    @Mapping(source = "reviewPerson.id",target = "reviewPerson.personId")
    @Mapping(source = "reviewPerson.job",target = "reviewPerson.post")
    @Mapping(target = "hiddenDangerSource",constant = "QLT")
    @Mapping(source = "hiddenDangerContentTypeName",target ="hiddenDangerCategory")
    @Mapping(source = "overStatusName",target ="overdueStatus")
    @Mapping(source = "fileResponses",target = "images",qualifiedByName = "fileResponsesToImages")
    @Mapping(source = "hiddenDangerExplain",target = "explainInfo")
    HiddenDangerOperateDetailResponse qualityHiddenDangerResponseToHiddenDangerOperateDetailResponse(QualityHiddenDangerResponse qualityHiddenDangerResponse);



    @Named("fileResponsesToImages")
    default String fileResponsesToImages(List<FileResponse> fileResponses){
        if (!VerifyHelper.isAvailable(fileResponses)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        fileResponses.stream().forEach(item -> sb.append(item.getUrl()).append(","));
        return sb.toString().substring(0,sb.toString().length()-1);
    }

    @Mapping(source = "hiddenDangerStatus",target = "taskStatus")
    @Mapping(source = "hiddenDangerCheckType",target = "checkType")
    @Mapping(source = "checkStartTime",target = "checkStartDate")
    @Mapping(source = "checkEndTime",target = "checkEndDate")
    QueryQualityHiddenDangerPageRequest availableHiddenDangerBySafetyRequestToQueryQualityHiddenDangerPageRequest(AvailableHiddenDangerBySafetyRequest availableHiddenDangerBySafetyRequest);


    PaginationData<HiddenDangerResponse> qualityHiddenDangerResponseToHiddenDangerResponse(PaginationData<QualityHiddenDangerResponse> qualityHiddenDangerResponse);

    @Mapping(source ="hiddenDangerCheckType",target = "checkType")
    QueryHiddenDangerByCheckRequest availableHiddenDangerBySafetyRequestToQueryHiddenDangerByCheckRequest(AvailableHiddenDangerBySafetyRequest availableHiddenDangerBySafetyRequest);
}
